<?php

use yii\helpers\Html;
use kartik\grid\GridView;
use common\models\business\BusinessContractPurchase;
/* @var $this yii\web\View */
/* @var $searchModel backend\modules\business\models\BusinessContractPurchaseSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '购进单管理';
$this->params['breadcrumbs'][] = $this->title;
?>


<section class="wrapper site-min-height">
    <!-- page start-->
    <section class="panel">
        <header class="panel-heading">
              <?= Html::a('返回','javascript:history.back(-1)'); ?>  <?= Html::encode($this->title) ?>
        </header>
        <div class="panel-body">
            <div class="adv-table editable-table ">
 


<div class="business-contract-purchase-index">

    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>


    <p>
        <?= Html::a('添加购进单', ['create'], ['class' => 'btn btn-success']) ?>
    </p>

    <?php
        echo GridView::widget([
            'dataProvider'=>$dataProvider,
            'filterModel'=>$searchModel,
            'showPageSummary'=>true,
            'pjax'=>false,
            'striped'=>true,
            'hover'=>true,
            'panel'=>['type'=>'primary', 'heading'=>'购进单管理'],
            'containerOptions'=>['style'=>'overflow: auto'], // only set when $responsive = false
            'headerRowOptions'=>['class'=>'kartik-sheet-style'],
            'filterRowOptions'=>['class'=>'kartik-sheet-style'],
            'columns'=>[
                [
                    'attribute'=>'id',
                    'pageSummaryOptions'=>['class'=>'text-right text-warning'],
                    'width'=>'20px',
                ],
                [
                    'label' => '所属合同',
                    'attribute'=>'contractname',
                    'value' => 'contract.name',
                    'width'=>'100px',
                    'pageSummaryOptions'=>['class'=>'text-right text-warning'],
                ],
                [
                    'attribute'=>'statements.name',
                    'pageSummaryOptions'=>['class'=>'text-right text-warning'],
                    'width'=>'100px',
                ],

                [
                    'attribute'=>'name',
                    'pageSummaryOptions'=>['class'=>'text-right text-warning'],
                    'width'=>'100px',
                ],

                [
                    'attribute'=>'num',
                    'pageSummaryOptions'=>['class'=>'text-right text-warning'],
                    'width'=>'60px',
                    'pageSummary'=>true
                ],
                [
                    'attribute'=>'price',
                    'pageSummaryOptions'=>['class'=>'text-right text-warning'],
                    'width'=>'60px',
                    'format'=>['decimal', 0],
                    'pageSummary'=>true
                ],
                [
                    'attribute'=>'createtime',
                    'value' => function($model){
                        return date('Y-m-d',$model->createtime);
                    },
                    'width'=>'120px',
                    'hAlign'=>'right',
                    'filterType' => GridView::FILTER_DATE_RANGE,
                    'filterWidgetOptions' =>([
                        'attribute'=>'createtime',
                        'presetDropdown'=>false,
                        'convertFormat'=>true,
                        'pluginOptions'=>[
                            'separator'=>'至',
                            'format'=>'Y-m-d',
                            'opens'=>'left',
                        ],
                    ])
                ],
                [
                    'label' => '是否结算',
                    'format' => 'raw',
                    'value' => function($model){
                        if(empty($model->statements)){
                            return Html::label('未结算','',['class'=>'btn btn-default btn-xs']);
                        }else{
                            return Html::label('已经算','',['class'=>'btn btn-success btn-xs']);
                        }
                    },
                    'attribute'=>'isjs',
                    'pageSummaryOptions'=>['class'=>'text-right text-warning'],
                    'width'=>'80px',
                ],
                [
                    'attribute'=>'status',
                    'width'=>'60px',
                    'format' => 'raw',
                    'value'=>function ($model, $key, $index, $widget) {
                        if($model->status==BusinessContractPurchase::STATUS_DEFAULT){
                            return Html::label(BusinessContractPurchase::$statues[$model->status], '', ['class' => 'btn btn-xs btn-default']);
                        }else if($model->status==BusinessContractPurchase::STATUS_AGREE){
                            return Html::label('已审核', '', ['class' => 'btn btn-xs btn-success']);
                        }else if($model->status==BusinessContractPurchase::STATUS_FINISH){
                            return Html::label('已结算', '', ['class' => 'btn btn-xs btn-primary']);
                        }

                        return Html::label(BusinessContractPurchase::$statues[$model->status], '', ['class' => 'btn btn-xs btn-default']);
                    },
                    'filterType'=>GridView::FILTER_SELECT2,
                    'filter'=> BusinessContractPurchase::$statues,
                    'filterWidgetOptions'=>[
                        'pluginOptions'=>['allowClear'=>true],
                    ],
                    'filterInputOptions'=>['placeholder'=>'选择状态']
                ],
                [
                    'class' => '\kartik\grid\ActionColumn',
                    'template' => '{view} {update} {delete} {agree}',
                    'buttons' => [
                        'agree' => function ($url, $model) {
                            return Html::a('审核通过','/business/contract-purchase/agree?id='.$model->id,[
                                'title' => '审核通过','class'=>'purchase','data-id'=>$model->id,
                            ]);
                        }
                    ],
                ],
            ],
            'toolbar'=> [
                '{export}',
                '{toggleData}',
            ],
            'bordered'=>true,
            'condensed'=>true,
        ]);
    ?>

</div>
</div>
</div>
</section>
</section>




<script>
    <?php $this->beginBlock('js_end') ?>
    $('#addstatements').click(function () {
        var keys = jqchk();
        console.log(keys);
        if(keys.length == 0){
            alert('请选择购进单');
            return false;
        }
        ids  = keys.join(',');
        var requestUrl = '<?php echo \yii\helpers\Url::to(['/business/contract-statements/create'])?>';
        window.location.href=requestUrl+'?'+'ids='+ids
    });

    function jqchk() { //jquery获取复选框值=
        var chk_value = [];
        $('input[name="selection[]"]:checked').each(function () {
            chk_value.push($(this).val());
        });
        return chk_value;
    }
    <?php $this->endBlock(); ?>
    <?php $this->registerJs($this->blocks['js_end'],\yii\web\View::POS_END);//将编写的js代码注册到页面底部 ?>
</script>






